package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;

import com.easy.bean.*;
import com.easy.service.*;
import com.easy.util.*;

//@RestController
//@RequestMapping("/reserve")
//@CrossOrigin
public class ReserveController1 {

   // @Autowired
    private ReserveService reserveService;



    @GetMapping
    public Result getAll() {
        MPJLambdaWrapper<Reserve> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Reserve.class)
                .selectAssociation(Member.class,Reserve::getMember)
                .selectAssociation(Course.class,Reserve::getCourse)
                .leftJoin(Member.class, Member::getId, Reserve::getMemberId)
                .leftJoin(Course.class, Course::getId, Reserve::getCourseId)
                .like(Member::getName, "娃")
                .or()
                .like(Course::getName, "娃")
                ;

        return Result.success(reserveService.list( wrapper));
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(name = "search", required = false) String searchtext) {
        QueryWrapper wrapper =null;
        if (searchtext != null && !searchtext.isEmpty()) {
            wrapper = new QueryWrapper<>();
        }
        return Result.success(reserveService.page(page, wrapper));
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success(reserveService.getById(id));
    }

    @PostMapping
    public Result create(@RequestBody Reserve entity) {
        return Result.success("数据已经存储",reserveService.save(entity));
    }

    @PutMapping
    public Result update( @RequestBody Reserve entity) {
        return Result.success("数据已经更新",reserveService.saveOrUpdate(entity));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success("数据已经删除",reserveService.removeById(id));
    }
}
